package com.yosaa.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yosaa.back.entity.FitnessProject;
import com.yosaa.back.service.IFitnessProjectService;
import com.yosaa.back.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 体测项目表，存储系统支持的所有体测项目及其详细信息 前端控制器
 * </p>
 *

 */
@RestController
@RequestMapping("/fitness-project")
public class FitnessProjectController {

    @Autowired
    private IFitnessProjectService fitnessProjectService;

    @GetMapping("/list")
    public ResponseEntity getFitnessProjectList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String projectName,
            @RequestParam(required = false) String unit) {

        Page<FitnessProject> projectPage = new Page<>(page, size);

        QueryWrapper<FitnessProject> queryWrapper = new QueryWrapper<>();
        if (projectName != null && !projectName.trim().isEmpty()) {
            queryWrapper.like("project_name", projectName);
        }
        if (unit != null && !unit.trim().isEmpty()) {
            queryWrapper.eq("unit", unit);
        }

        IPage<FitnessProject> result = fitnessProjectService.page(projectPage, queryWrapper);
        return ResponseEntity.ok(new ApiResponse(0, result, "操作成功"));
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse> addFitnessProject(@RequestBody FitnessProject fitnessProject) {
        boolean saved = fitnessProjectService.save(fitnessProject);
        if (saved) {
            return ResponseEntity.ok(new ApiResponse(0, null, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse(1, null, "Failed to add fitness project"));
        }
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse> updateFitnessProject(@RequestBody FitnessProject fitnessProject) {
        boolean updated = fitnessProjectService.updateById(fitnessProject);
        if (updated) {
            return ResponseEntity.ok(new ApiResponse(0, null, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse(1, null, "操作失败"));
        }
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<ApiResponse> getFitnessProject(@PathVariable Integer id) {
        FitnessProject fitnessProject = fitnessProjectService.getById(id);
        if (fitnessProject != null) {
            return ResponseEntity.ok(new ApiResponse(0, fitnessProject, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(new ApiResponse(1, null, "操作失败"));
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<ApiResponse> deleteFitnessProject(@PathVariable Integer id) {
        boolean deleted = fitnessProjectService.removeById(id);
        if (deleted) {
            return ResponseEntity.ok(new ApiResponse(0, null, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse(1, null, "操作失败"));
        }
    }
}